package com.bringway.global.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bringway.global.dao.MenuDao;
import com.bringway.global.dao.UserDao;
import com.bringway.global.service.MenuService;
import com.bringway.p2p.dao.MenuCUDDao;

import net.sf.json.JSONObject;

/*
* @author 作者 xuef
* @version 创建时间：2015年9月9日 下午2:35:22
* 类说明
*/
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	private MenuDao menuDao;
	@Autowired
	private MenuCUDDao menuCudDao;
	@Autowired
	private UserDao userDao;

	@Override
	public List<Map<String, Object>>  listMenuByParent(JSONObject params) {
		String sql = " select t1.ID,t1.NAME,t1.URL,t1.PARENT_ID,case when 1=1 then (select count(*) CHILDCT from  `d_ccb_fm`.`t_fm_sys_menu` t2 where t2.PARENT_ID =t1.id ) end CHILDCT from  `d_ccb_fm`.`t_fm_sys_menu` t1 where id in(select (t2.MENU_ID) from  `d_ccb_fm`.`t_fm_sys_user_menu` t2 where t2.user_id = :id ) and t1.PARENT_ID = :nodeId    order by CHILDCT desc,id asc  ";
		return menuCudDao.query(sql, params);
//		return menuDao.listMenuByParent(params);
	}

	@Override
	public List<Map<String, Object>> queryRoleList(JSONObject params) {
		return userDao.queryRoleList(params);
	}

	@Override
	public void saveMenuRole(JSONObject params) {
		menuDao.saveMenu(params);
		menuDao.deleteMenuRole(params);
		String menuRoleIds = params.optString("menuRoleIds");
		String menuRoleNames = params.optString("menuRoleNames");
		String[] roleIds = menuRoleIds.split(",");
		String[] roleNames = menuRoleNames.split(",");
		for(int i=0;i<roleIds.length;i++){
			if(StringUtils.isEmpty(roleIds[i])) continue;
			String menuId = params.optString("menuId");
			menuDao.saveMenuRole(menuId,roleIds[i],roleNames[i]);
		}
	}

	@Override
	public String queryMenuRole(JSONObject params) {
		StringBuffer result = new StringBuffer();
		List<Map<String, Object>> list = menuDao.queryMenuRoleList(params);
		for(int i = 0;i<list.size();i++){
			Map map = list.get(i);
			String roleId =  map.get("ROLE_ID").toString();
			if(StringUtils.isEmpty(roleId)){
				continue;
			}
			if(i!=0){
				result.append(",");
			}
			result.append(roleId);
		}
		return result.toString();
	}

	@Override
	public List<Map<String, Object>> queryMenuRoleList(JSONObject params) {
		return menuDao.queryMenuRoleList(params);
	}

	@Override
	public Map<String, Map<String, Object>> listMenuByParent01(JSONObject params) {
		return menuDao.listMenuMapByParent01(params);
	}

	@Override
	public Map<String, Map<String,Object>> listMenuByRole(JSONObject params) {
		return menuDao.listMenuMapByRole(params);
	}

	@Override
	public List getRoleListByMenuId(JSONObject parms) {
		return menuDao.getRoleListByMenuId(parms);
	}
	

}
